package com.example.learn.hwod;

import java.util.Scanner;

public class _22工作安排 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);


        // 读入 T 和 n
        int T = scanner.nextInt();
        int n = scanner.nextInt();

        // 定义工作数组
        int[] time = new int[n];
        int[] reward = new int[n];

        // 读入每项工作的耗时和报酬
        for (int i = 0; i < n; i++) {
            time[i] = scanner.nextInt();
            reward[i] = scanner.nextInt();
        }

        // dp[i] 表示时间限制为 i 时可以获得的最大报酬
        int[] dp = new int[T + 1];



        // 动态规划填充 dp 数组
        for (int i = 0; i < n; i++) {
            for (int j = T; j >= time[i]; j--) {
                System.out.println(dp[j]);
                dp[j] = Math.max(dp[j], dp[j - time[i]] + reward[i]);
            }
        }

        // 输出最大报酬
        System.out.println(dp[T]);

        scanner.close();
    }
}
